package com.example.note.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.note.R;
import com.example.note.database.ReminderEntity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class ReminderAdapter extends RecyclerView.Adapter<ReminderAdapter.ReminderViewHolder> {
    
    private Context context;
    private List<ReminderEntity> reminders;
    private SimpleDateFormat dateTimeFormat;
    
    private OnReminderClickListener onReminderClickListener;
    private OnReminderCompleteListener onReminderCompleteListener;
    
    public interface OnReminderClickListener {
        void onReminderClick(ReminderEntity reminder);
    }
    
    public interface OnReminderCompleteListener {
        void onReminderComplete(ReminderEntity reminder);
    }
    
    public ReminderAdapter(Context context) {
        this.context = context;
        this.reminders = new ArrayList<>();
        this.dateTimeFormat = new SimpleDateFormat("MM-dd HH:mm", Locale.getDefault());
    }
    
    public void setReminders(List<ReminderEntity> reminders) {
        this.reminders = reminders != null ? reminders : new ArrayList<>();
        notifyDataSetChanged();
    }
    
    public void setOnReminderClickListener(OnReminderClickListener listener) {
        this.onReminderClickListener = listener;
    }
    
    public void setOnReminderCompleteListener(OnReminderCompleteListener listener) {
        this.onReminderCompleteListener = listener;
    }
    
    @NonNull
    @Override
    public ReminderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_reminder, parent, false);
        return new ReminderViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull ReminderViewHolder holder, int position) {
        ReminderEntity reminder = reminders.get(position);
        holder.bind(reminder);
    }
    
    @Override
    public int getItemCount() {
        return reminders.size();
    }
    
    class ReminderViewHolder extends RecyclerView.ViewHolder {
        
        private TextView tvTitle;
        private TextView tvContent;
        private TextView tvReminderTime;
        private ImageButton btnComplete;
        
        public ReminderViewHolder(@NonNull View itemView) {
            super(itemView);
            
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvContent = itemView.findViewById(R.id.tvContent);
            tvReminderTime = itemView.findViewById(R.id.tvReminderTime);
            btnComplete = itemView.findViewById(R.id.btnComplete);
            
            itemView.setOnClickListener(v -> {
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION && onReminderClickListener != null) {
                    onReminderClickListener.onReminderClick(reminders.get(position));
                }
            });
            
            btnComplete.setOnClickListener(v -> {
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION && onReminderCompleteListener != null) {
                    onReminderCompleteListener.onReminderComplete(reminders.get(position));
                }
            });
        }
        
        public void bind(ReminderEntity reminder) {
            tvTitle.setText(reminder.getTitle());
            
            if (!TextUtils.isEmpty(reminder.getContent())) {
                tvContent.setVisibility(View.VISIBLE);
                tvContent.setText(reminder.getContent());
            } else {
                tvContent.setVisibility(View.GONE);
            }
            
            if (reminder.getReminderTime() > 0) {
                tvReminderTime.setVisibility(View.VISIBLE);
                Date reminderDate = new Date(reminder.getReminderTime());
                tvReminderTime.setText("⏰ " + dateTimeFormat.format(reminderDate));
                
                // 检查是否过期
                if (reminder.getReminderTime() < System.currentTimeMillis() && !reminder.isCompleted()) {
                    tvReminderTime.setTextColor(context.getResources().getColor(android.R.color.holo_red_dark));
                } else {
                    tvReminderTime.setTextColor(context.getResources().getColor(android.R.color.darker_gray));
                }
            } else {
                tvReminderTime.setVisibility(View.GONE);
            }
            
            // 根据完成状态设置按钮图标
            if (reminder.isCompleted()) {
                btnComplete.setImageResource(R.drawable.ic_check_circle);
                btnComplete.setEnabled(false);
                itemView.setAlpha(0.6f);
            } else {
                btnComplete.setImageResource(R.drawable.ic_check_circle_outline);
                btnComplete.setEnabled(true);
                itemView.setAlpha(1.0f);
            }
        }
    }
}